# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
_luaversions="5.1 5.2 5.3 5.4"
pkgname=lua-iconv
pkgver=7
pkgrel=4
pkgdesc="Lua binding to the POSIX 'iconv' library"
url="http://ittner.github.io/lua-iconv/"
arch="all"
license="MIT"
for _i in $_luaversions; do
	makedepends="$makedepends lua$_i-dev"
	subpackages="$subpackages lua$_i-iconv:_split"
done
source="$pkgname-$pkgver-2.tar.gz::https://github.com/ittner/lua-iconv/archive/refs/tags/lua-iconv-$pkgver.tar.gz
	0001-lua-iconv-make-close-available-from-lua.patch
	"
builddir="$srcdir/lua-iconv-lua-iconv-$pkgver"
options="!check"

prepare() {
	default_prepare
	for _i in $_luaversions; do
		cp -r "$builddir" "$srcdir"/"$_i"
	done
}

build() {
	for _i in $_luaversions; do
		msg "Build for Lua $_i"
		cd "$srcdir"/$_i
		make LUAPKG="lua$_i" \
			CFLAGS="$CFLAGS $(pkg-config lua$_i --cflags)"
	done
}

package() {
	cd "$srcdir"
	for _i in $_luaversions; do
		install -D -s $_i/iconv.so "$pkgdir"/usr/lib/lua/$_i/iconv.so
	done
}

_split() {
	local d=usr/lib/lua _ver=${subpkgname%%-*}
	_ver=${_ver#lua}
	pkgdesc="$pkgdesc for Lua $_ver"
	install_if="lua$_ver $pkgname=$pkgver-r$pkgrel"
	depends=
	mkdir -p "$subpkgdir/$d"
	mv "$pkgdir/$d/$_ver" "$subpkgdir/$d"
}

sha512sums="
7fae8be68878747a6bcb559d1548f7d0b330d519cdf306f6ec8250cb914ed971ba1a86fef20771a5add728d796c613c32544c20ba4f8b4315d20ee8ac39a607f  lua-iconv-7-2.tar.gz
472e6611638d57c6c99fcfe53defa9766d89c45f6eec5fb4dd31bd63a119901a99fa2219d38d66644d00c0fcfd006418ccb3ee5c0496f2397d3d76d05ae0cee9  0001-lua-iconv-make-close-available-from-lua.patch
"
